﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package reprise.core {
    import reprise.ui.*;
    import flash.events.*;
    import flash.display.*;
    import reprise.utils.*;
    import flash.utils.*;
    import flash.ui.*;

    public class FocusManager {

        protected var m_focus:UIObject;
        protected var m_document:DocumentView;
        protected var m_inFocusHandling:Boolean;
        protected var m_lastTabPress:int;

        public static const FOCUS_METHOD_MOUSE:String = "mouse";
        public static const FOCUS_METHOD_KEYBOARD:String = "keyboard";

        public function FocusManager(_arg1:DocumentView){
            m_document = _arg1;
            _arg1.addEventListener(FocusEvent.KEY_FOCUS_CHANGE, document_keyFocusChange);
            _arg1.addEventListener(FocusEvent.MOUSE_FOCUS_CHANGE, document_mouseFocusChange);
            _arg1.addEventListener(FocusEvent.FOCUS_IN, document_focusIn);
            _arg1.addEventListener(MouseEvent.MOUSE_DOWN, document_mouseDown);
        }
        protected function document_mouseDown(_arg1:MouseEvent):void{
            var _local2:UIObject = DisplayListUtil.locateElementContainingDisplayObject(DisplayObject(_arg1.target), true);
            if (_local2 == m_focus){
                return;
            };
            setFocusedElement(_local2, FOCUS_METHOD_MOUSE);
        }
        public function setFocusedElement(_arg1:UIObject, _arg2:String):Boolean{
            if (((m_focus) && ((m_focus == _arg1)))){
                return (false);
            };
            if (m_focus){
                m_focus.setFocus(false, _arg2);
            };
            m_focus = _arg1;
            if (((_arg1) && ((_arg1.document == m_document)))){
                _arg1.setFocus(true, _arg2);
                return (true);
            };
            m_focus = null;
            return (false);
        }
        protected function document_keyFocusChange(_arg1:FocusEvent):void{
            var _local2:UIObject;
            if (m_inFocusHandling){
                return;
            };
            m_inFocusHandling = true;
            if ((((_arg1.keyCode == Keyboard.TAB)) && (!(_arg1.isDefaultPrevented())))){
                if ((getTimer() - m_lastTabPress) < 15){
                    _arg1.preventDefault();
                    m_inFocusHandling = false;
                    return;
                };
                m_lastTabPress = getTimer();
                if (_arg1.shiftKey){
                    _local2 = ((m_focus)!=null) ? m_focus.previousValidKeyView() : m_document.previousValidKeyView();
                } else {
                    _local2 = ((m_focus)!=null) ? m_focus.nextValidKeyView() : m_document.nextValidKeyView();
                };
                if (setFocusedElement(_local2, FOCUS_METHOD_KEYBOARD)){
                    _arg1.preventDefault();
                };
            };
            m_inFocusHandling = false;
        }
        protected function document_mouseFocusChange(_arg1:FocusEvent):void{
            if (m_inFocusHandling){
                return;
            };
            m_inFocusHandling = true;
            var _local2:DisplayObject = DisplayObject(_arg1.relatedObject);
            var _local3:UIObject = DisplayListUtil.locateElementContainingDisplayObject(_local2, true);
            if (setFocusedElement(_local3, FOCUS_METHOD_MOUSE)){
                _arg1.preventDefault();
            };
            m_inFocusHandling = false;
        }
        protected function document_focusIn(_arg1:FocusEvent):void{
            if (m_inFocusHandling){
                return;
            };
            m_inFocusHandling = true;
            var _local2:UIObject = DisplayListUtil.locateElementContainingDisplayObject(DisplayObject(_arg1.target), true);
            if (m_focus != _local2){
                if (setFocusedElement(_local2, FOCUS_METHOD_MOUSE)){
                    _arg1.preventDefault();
                };
            };
            m_inFocusHandling = false;
        }

    }
}//package reprise.core 
